--
-- User_Agent鉴权.
--

local cloak = require("src.cloak")

local fmt = string.format

--- 检查User_Agent
--
local function access()
  local headers = ngx.req.get_headers()
  local user_agent = headers and (headers["user-agent"] or headers["User-Agent"] or headers["User-agent"] or headers["user-Agent"])
  ngx.log(ngx.DEBUG, fmt("Req user_agent= %s", user_agent))

  user_agent = string.lower(user_agent)
  local bool = true
  for _, v in pairs(cloak.get_user_agent_rules()) do
    local idx = string.find(user_agent, v)
    if idx ~= nil then
      bool = false
    end
  end
  ngx.ctx.is_filter_user_agnet = bool
end

return {
  access = access
}